﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Drawing;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for MemberGUI.xaml
    /// </summary>
    public partial class MemberGUI : UserControl
    {
        private readonly ImageSource defaultImage;
        private eKanbanWPF.Model.Member member = null;

        public event EventHandler Changed;

        public delegate void PositionChangedHandler(MemberGUI memberGui);
        public event PositionChangedHandler PositionChanged;

        void member_Changed(eKanbanWPF.Model.Member sender)
        {
            if (sender != null)
            {
                ToolTip = sender.Name;
                Width = sender.Width;
                Height = sender.Height;
                Canvas.SetLeft(this, sender.Left);
                Canvas.SetTop(this, sender.Top);


                if (sender.Image != null)
                {
                    //this.image.Source = (ImageSource)sender.Image;
                    this.image.Source = BitmapConversion.ToWpfBitmap((Bitmap)sender.Image);
                }
                else
                {
                    this.image.Source = defaultImage;
                }
            }
            else
            {
                ToolTip = "";
            }
            if (Changed != null)
            {
                Changed.Invoke(this, null);
            }
        }

        private void setMember(eKanbanWPF.Model.Member value)
        {
            if (member != null)
            {
                member.Changed -= member_Changed;
            }
            member = value;
            if (member != null)
            {
                member.Changed += member_Changed;
            }
            member_Changed(value);
        }


        public eKanbanWPF.Model.Member Member
        {
            get { return member; }
            set
            {
                setMember(value);
            }
        }

        public MemberGUI()
        {
            InitializeComponent();
            defaultImage = this.image.Source;
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            member.Left = Canvas.GetLeft(this);
            member.Top = Canvas.GetTop(this);
            var r = FrmMember.Show(member);
            if (r == true)
            {
                member.Name = FrmMember.MemberName;
                member.Comment = FrmMember.MemberComment;
                member.Sex = FrmMember.MemberSex;
                member.Image = FrmMember.MemberImage;
            }
        }

        private void ContentControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, true);
            ((Control)sender).Focus();
        }

        private void ContentControl_LostFocus(object sender, RoutedEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, false);
            if (member != null)
            {
                bool ck = false;
                if (member.Left != Canvas.GetLeft(this) || member.Top != Canvas.GetTop(this))
                {
                    ck = true;
                }

                member.Width = this.Width;
                member.Height = this.Height;
                member.Left = Canvas.GetLeft(this);
                member.Top = Canvas.GetTop(this);

                if (Changed != null)
                {
                    Changed.Invoke(this, null);
                }

                if (ck) 
                {
                    PositionChanged.Invoke(this);
                }
            }
        }

        private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show(Canvas.GetLeft(this).ToString());
            if (member != null)
            {
                bool ck = false;
                if (member.Left != Canvas.GetLeft(this) || member.Top != Canvas.GetTop(this))
                {
                    ck = true;
                }
                member.Width = this.Width;
                member.Height = this.Height;
                member.Left = Canvas.GetLeft(this);
                member.Top = Canvas.GetTop(this);

                if (Changed != null)
                {
                    Changed.Invoke(this, null);
                }

                if (ck)
                {
                    PositionChanged.Invoke(this);    
                }
                
            }
        }

    }
}
